package my.graduation.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import my.graduation.dto.DepartInfoDto;
import my.graduation.entity.Clinic;
import my.graduation.entity.Department;
import my.graduation.enums.HttpCodeEnums;
import my.graduation.mapper.ClinicMapper;
import my.graduation.mapper.DepartmentMapper;
import my.graduation.result.Result;
import my.graduation.service.DepartmentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: RavenLyn
 * @Date: 2024-12-09
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {

    @Override
    public Result<List<Department>> getDepartList() {
        return Result.success(list());
    }

    /**
     * 条件获取科室信息
     * @param dto
     * @return
     */
    @Override
    public Result<List<Department>> getListByCon(DepartInfoDto dto) {
        List<Department> departmentList = list(Wrappers.<Department>lambdaQuery()
                .like(StringUtils.isNotBlank(dto.getName()), Department::getName, dto.getName()));

        return Result.success(departmentList);
    }

    /**
     * 新增科室信息
     * @return
     */
    @Override
    public Result add(DepartInfoDto dto) {
        //1 非空
        if (StringUtils.isNotBlank(dto.getName()) &&
                StringUtils.isNotBlank(dto.getDescription())) {
            Department department = new Department();
            BeanUtils.copyProperties(dto, department);
            boolean i = save(department);
            if (i) {
                return Result.success();
            } else {
                return Result.error(HttpCodeEnums.SYSTEM_ERROR);
            }
        }else{
            return Result.error(HttpCodeEnums.INFO_IS_BLANK);

        }
    }

    /**
     * 修改科室信息
     * @return
     */
    @Override
    public Result update(DepartInfoDto dto) {
        //1 非空
        if (StringUtils.isNotBlank(dto.getName()) &&
                StringUtils.isNotBlank(dto.getDescription()) &&
                dto.getId()!=null) {
            Department department = new Department();
            BeanUtils.copyProperties(dto, department);
            boolean i = updateById(department);
            if (i) {
                return Result.success();
            } else {
                return Result.error(HttpCodeEnums.SYSTEM_ERROR);
            }
        }else{
            return Result.error(HttpCodeEnums.INFO_IS_BLANK);

        }
    }

    @Autowired
    private ClinicMapper clinicMapper;

    /**
     * 删除科室信息
     * @return
     */
    @Override
    public Result delete(DepartInfoDto dto) {
        //1 非空
        if (dto.getId()!=null) {
            List<Clinic> clinicList = clinicMapper.selectList(Wrappers.<Clinic>lambdaQuery().eq(Clinic::getDepartmentId, dto.getId()));
            if(!clinicList.isEmpty()){
                return Result.error(HttpCodeEnums.ILLEGAL_OPERATION);
            }
            Department department = new Department();
            BeanUtils.copyProperties(dto, department);
            boolean i = removeById(department);
            if (i) {
                return Result.success();
            } else {
                return Result.error(HttpCodeEnums.SYSTEM_ERROR);
            }
        }else{
            return Result.error(HttpCodeEnums.INFO_IS_BLANK);
        }
    }
}
